import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import router from 'umi/router';

import {
  queryCellList,
  queryCellConf,
  queryWhiteCourier,
  queryOtherCourier,
  addCourier,
  deleteCourier,
  saveCellConf,
} from './service';

import { fetchValidation } from '@/utils';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetchCellList: Effect;
    fetchCellConf: Effect;
    fetchWhiteCourier: Effect;
    fetchOtherCourier: Effect;
    addCourier: Effect;
    deleteCourier: Effect;
    saveCellConf: Effect;
  };
  reducers: {
    save: Reducer;
  };
}

const stateInfo = {
  cellInfo: [],
  cellList: {},
  dataInfo: {
    list: [],
    count: 0,
  },
  courierInfo: {
    list: [],
    count: 0,
  },
};

const Model: ModelType = {
  namespace: 'deliveryWhiteList',

  state: stateInfo,

  effects: {
    * fetchCellList({ payload }, { call, put }) {
      const response = yield call(queryCellList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'cellList',
          value: response.body?.cellCount,
        },
      });
    },

    * fetchCellConf({ payload }, { call, put }) {
      const response = yield call(queryCellConf, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'cellInfo',
          value: response.body,
        },
      });
    },

    * fetchWhiteCourier({ payload }, { call, put }) {
      const response = yield call(queryWhiteCourier, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'dataInfo',
          value: response.body,
        },
      });
    },

    * fetchOtherCourier({ payload }, { call, put }) {
      const response = yield call(queryOtherCourier, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'courierInfo',
          value: response.body,
        },
      });
    },

    * addCourier({ payload }, { call, put }) {
      const response = yield call(addCourier, payload);
      const data = fetchValidation(response);
      if (data) {
        message.success('添加成功');
        yield put({
          type: 'fetchWhiteCourier',
          payload: { cabinetId: payload.cabinetId },
        });
      }
    },

    * deleteCourier({ payload: { id, ...query } }, { call, put }) {
      const response = yield call(deleteCourier, { id });
      const data = fetchValidation(response);
      if (data) {
        message.success('删除成功');
        yield data && put({
          type: 'fetchWhiteCourier',
          payload: query,
        });
      }
    },

    * saveCellConf({ payload }, { call }) {
      const response = yield call(saveCellConf, payload);
      const data = fetchValidation(response);
      if (data) {
        message.success('配置成功');
        router.goBack();
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        [payload.type]: payload.value,
      };
    },
  },
};

export default Model;
